package com.tiny.work.test.collections.hashmap;

import org.junit.Test;

import java.util.HashMap;

public class HashMapDemo {

    @Test
    public void  TestForInit(){
        HashMap<String, String> map = new HashMap<>();
        map.put("雪国","川端康成");
        map.put("月亮和六便士","毛姆");
        map.put("双城记","狄更斯");
        map.put("老人与海","海明威");
        map.put("在路上","凯鲁亚克");
        map.put("洛丽塔","纳博科夫");
        map.put("苏菲的抉择","斯泰伦");
        map.put("复活","托尔斯泰");
        map.put("悲惨世界","雨果");
        map.put("1984","乔治·奥威尔");
        map.put("浮士德","歌德");
        map.put("白夜行","东野圭吾");
        map.put("三体","刘慈欣");
        map.put("三体Ⅱ·黑暗森林","刘慈欣");
        //resize
        map.put("三体Ⅲ·死神永生","刘慈欣");
    }

    @Test
    public void  TestForInit1(){
        HashMap<String, String> map = new HashMap<>(4);
        map.put("双城记","狄更斯");
        map.put("浮士德","歌德");
        map.put("老人与海","海明威");
        map.put("在路上","凯鲁亚克");
        map.put("洛丽塔","纳博科夫");
        map.put("复活","托尔斯泰");
        // resize
        map.put("悲惨世界","雨果");
    }

    @Test
    public void test(){

        int i = hash("三体") & (16 - 1);
        i = hash("金色笔记") & (16 - 1);
        i = hash("法国中尉的女人") & (16 - 1);
        i = hash("大河湾") & (16 - 1);
        i = hash("千万别丢下我") & (16 - 1);
        int i1 = hash(new Key()) & (16 - 1);
        int i2 = hash(new Key()) & (16 - 1);

    }
    int hash(Object k) {
        int h = 0;
//        if (0 != h && k instanceof String) {
//            return sun.misc.Hashing.stringHash32((String) k);
//        }
//
//        h ^= k.hashCode();
//
//        // This function ensures that hashCodes that differ only by
//        // constant multiples at each bit position have a bounded
//        // number of collisions (approximately 8 at default load factor).
//        h ^= (h >>> 20) ^ (h >>> 12);
        return h ^ (h >>> 7) ^ (h >>> 4);
    }
    class Key{
        int value ;

        @Override
        public int hashCode() {
            return 1;
        }
    }
    @Test
    public void test_bit(){
        char[] intToArray = IntToArray(1282180);//
    }

    public char [] IntToArray(int data){
        char []arrays = new char[32];
        StringBuffer result = new StringBuffer();
        int i = 0 ;
        while (data > 0) {
            arrays[i] = (char) ((data) & 1);
            data = data>>1;
            i++;
        }
        return arrays;
    }
}
